package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;

/**
 * 取派员的业务实现类
 * 
 * @author Administrator
 *
 */
@Service
@Transactional
public class CourierServiceImpl implements CourierService {

	/** 注入dao */
	@Autowired
	private CourierRepository courierRepository;
	
	/**
	 * @see cn.itcast.bos.service.base.CourierService#save(Courier)
	 */
	@Override
	public void save(Courier courier) {

		courierRepository.save(courier);
	}

	/**
	 * @see cn.itcast.bos.service.base.CourierService#findPageData(Pageable)
	 */
	@Override
	public Page<Courier> findPageData(Specification<Courier> specification, Pageable pageable) {
		
		return courierRepository.findAll(specification,pageable);
	}

	/**
	 * @see cn.itcast.bos.service.base.CourierService#del(String[])
	 */
	@Override
	public void del(String[] idArray) {

		// 调用dao实现update修改操作,将deltag修改为1 
		for (String str : idArray) {
			Integer id = Integer.parseInt(str);
			courierRepository.updateDelTag(id);
		}
	}

	/**
	 * @see cn.itcast.bos.service.base.CourierService#res(String[])
	 */
	@Override
	public void res(String[] idArray) {
		// 调用dao实现update修改操作,将deltag修改为null 
				for (String str : idArray) {
					Integer id = Integer.parseInt(str);
					courierRepository.resDelTag(id);
				}
	}

	/**
	 * @see cn.itcast.bos.service.base.CourierService#findNoAssociation()
	 */
	@Override
	public List<Courier> findNoAssociation() {
		// 封装specification
		Specification<Courier> specification = new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 查询条件, 判定列表size为空  isEmpty判断集合的   isnotblack是判断字段的
				Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				
				return p;
			}
		};
		
		return courierRepository.findAll(specification);
	}

}
